package reduce.fz.com.reduce.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.HashMap;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.DetailUserFollowView;
import reduce.fz.com.reduce.custom.delegate.RecyclerViewDelegate;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.service.UserService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ServiceBuilder;

/**
 * 用户关注
 */
public class AttentionListFragment extends BaseListFragment<AttentionListFragment.AttentionViewHolder, User> {
    private UserService userService = ServiceBuilder.builderUserService(null);
    //0关注 1粉丝
    public int followtype = 0;
    private Long uid;


    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<User>>(new TypeReference<Result<List<User>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                loadingDialog.dismiss();
            }

            @Override
            public void responseInfo(Result<List<User>> data) {
                addResult(data);
            }
        });
    }


    public static AttentionListFragment create(int position, int type, Long uid) {
        AttentionListFragment fragment = new AttentionListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_FRAGMENT_INDEX, position);
        bundle.putInt("followType", type);
        bundle.putLong("uid", uid);
        fragment.setArguments(bundle);
        return fragment;
    }
    public static AttentionListFragment create( int type, Long uid) {
        AttentionListFragment fragment = new AttentionListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("followType", type);
        bundle.putLong("uid", uid);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        followtype = getArguments().getInt("followType");
        uid = getArguments().getLong("uid");
    }

    public static AttentionListFragment create() {
        AttentionListFragment fragment = new AttentionListFragment();
        return fragment;
    }

    private RecyclerViewDelegate mRecyclerViewDelegate = new RecyclerViewDelegate();

    public boolean isViewBeingDragged(MotionEvent event) {
        return mRecyclerViewDelegate.isViewBeingDragged(event, super.recyclerView);
    }

    public interface AttentionListener {
        public void follow(User user);

        public void disfollow(User user);
    }


    public AttentionListFragment() {
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    @Override
    protected AttentionViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_attention_user, parent, false);
        final User srcuser = UserPreference.getUser();
        AttentionViewHolder holder = new AttentionViewHolder(view, followtype, new AttentionListener() {
            @Override
            public void follow(User user) {
                userService.follow(user.uid, true, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
                }) {
                    @Override
                    public void error(ServiceException e) {

                    }

                    @Override
                    public void responseInfo(Result<HashMap> data) {

                    }
                });
            }

            @Override
            public void disfollow(User user) {
                userService.follow(user.uid, false, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
                }) {
                    @Override
                    public void error(ServiceException e) {

                    }

                    @Override
                    public void responseInfo(Result<HashMap> data) {

                    }
                });

            }
        });
        return holder;
    }

    @Override
    protected AttentionViewHolder createfootViewHolder(View view) {

        return new AttentionViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<User> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_user_follow_list, uid, followtype, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return 0;
    }

    public static class AttentionViewHolder extends BaseRecycleViewHolder<User> {
        private RoundNetworkImageView mAvatar;
        private TextView mNickName;
        private DetailUserFollowView mFollow;
        private AttentionListener attentionListener;
        private TextView mintr;
        //0关注列表，1粉丝列表
        private int followtype = 0;

        public AttentionViewHolder(View itemView, int followtype, AttentionListener listener) {
            this(itemView);
            this.attentionListener = listener;
            this.followtype = followtype;

        }

        public AttentionViewHolder(View itemView) {
            super(itemView);
            mintr=(TextView)itemView.findViewById(R.id.tv_intr);
            mAvatar = (RoundNetworkImageView) itemView.findViewById(R.id.iv_user_avatar);
            mNickName = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            mFollow = (DetailUserFollowView) itemView.findViewById(R.id.dfv_follow);

        }

        @Override
        public void renderView(final User data, int position) {
            if(!StringUtils.isEmpty(data.intr)){
                mintr.setText(data.intr);
            }
            mAvatar.setImageUrl(data.avatar, HttpUtils.getAvatarImageLoader());
            mNickName.setText(data.nickname);
            mFollow.setStatus(UserPreference.getUser().uid, data);
            mFollow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mFollow.doToggleAction();
                }
            });

        }


    }


}
